<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>管理员列表</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/index.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/index.js"></script>
         <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

    </head>
    <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</a>
            <a>
              <cite>导航元素</cite></a>
          </span>
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input"  autocomplete="off" placeholder="开始日" name="start" id="start">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input"  autocomplete="off" placeholder="截止日" name="end" id="end">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="text" name="account"  placeholder="请输入用户名" autocomplete="off" id="account" class="layui-input">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="button"  value="搜索" class="layui-btn" id="addmiselect" onclick="aaa()" >
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-header">
                            <button class="layui-btn" onclick="xadmin.open('添加用户','./admin-add.html',600,400)"><i class="layui-icon"></i>添加</button>
                        </div>
                        <div class="layui-card-body ">
                            <table class="layui-table layui-form">
                              <thead>
                                <tr>
                                  <th>登录名</th>
                                  <th>角色</th>
                                  <th>加入时间</th>
                                  <th>状态</th>
                                  <th>操作</th>
                              </thead>
                              <tbody id="glytj">
                               
                                
                              </tbody>
                            </table>
                        </div>
                       
                       
                        <div class="layui-card-body " id="fenye">
                           
                        </div>
                    </div>
                </div>
            </div>
        </div> 
    </body>
    <script>
      layui.use(['laydate','form'], function(){
        var laydate = layui.laydate;
        var form = layui.form;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
      });

       /*用户-停用*/
      function member_stop(obj,id)
      {
         if($(obj).attr('title')=='启用')
 			{
 				 layer.confirm('确认要停用吗？',function(index){
 			
              state(obj,id);
          });
 			}else
 			{
 				 layer.confirm('确认要启用吗？',function(index){
 				state(obj,id)
              
          });
 			} 
         
      }


function state (obj,account) {
	var state = 1;
 			if($(obj).attr('title')=='启用')
 			{
 				state =0;
 			}else
 			{
 				state =1;
 			}
 			var a =
            {
            	"account":account,
            	"state":state,
            }
           
             $.ajax({
		    type: "post",//请求方式
		    url: "http://localhost:16111/user/update_Administrator_state",//上s层的路径
			dataType: "json",
		    data:{"account":account,
            	"state":state},//传值
		    xhrFields:{withCredentials: true},
		    success: function (data)
			{
				var user = eval('(' + JSON.stringify(data) + ')');
				if(user.data==1)
				{
					console.log("修改成功")
				}
		   }
		});
              if($(obj).attr('title')=='启用'){
				
                //发异步把用户状态进行更改
                $(obj).attr('title','停用')
                $(obj).find('i').html('&#xe62f;');

                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                layer.msg('已停用!',{icon: 5,time:1000});

              }else{
                $(obj).attr('title','启用')
                $(obj).find('i').html('&#xe601;');

                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                layer.msg('已启用!',{icon: 1,time:1000});
              }
}
      /*用户-删除*/
      function member_del(obj,account){
          layer.confirm('确认要删除吗？',function(index){
              //发异步删除数据
              
              
              $(obj).parents("tr").remove();
           
             $.ajax({
		    type: "post",//请求方式
		    url: "http://localhost:16111/user/delete_Administrator",//上s层的路径
			dataType: "json",
		    data:{"account":account},//传值
		    xhrFields:{withCredentials: true},
		    success: function (data)
			{
				var user = eval('(' + JSON.stringify(data) + ')');
				
		   }
		});
			
              layer.msg('已删除!',{icon:1,time:1000});
               
              setTimeout("window.location.reload()",1001)
          });
      }



      function delAll (argument) {

        var data = tableCheck.getData();
  
        layer.confirm('确认要删除吗？'+data,function(index){
            //捉到所有被选中的，发异步进行删除
           
            layer.msg('删除成功', {icon: 1});
            $(".layui-form-checked").not('.header').parents('tr').remove();
           
        });
      }
    </script>
 <script>
              var max = 0;
              var numberfenye = 9;
               var yeshu = 0; 
                var shuju = null ;
                						aaa()
                                	function aaa (){
                                		$.ajax({
									    type: "post",//请求方式
									    url: "http://localhost:16111/user/AdministratorSelect",//上s层的路径
										datatype: "json",
									    data:{start:$("#start").val(),end:$("#end").val(),account:$("#account").val()},//传值
									   	xhrFields:{withCredentials: true},
									   success: function (data)
										{

											var user = eval('(' + JSON.stringify(data) + ')');
											
											var str = "";
											
								              
											shuju = data;
											for(var i in user.data)
											{
												max +=1;
												if(i>=0 && i<=numberfenye-1)
												{
													str += "<tr>"+
				                                
				                                  "<td>"+user.data[i].account+"</td>"+
				                                  "<td>"+user.data[i].role_name+"</td>"+
				                                  "<td>"+user.data[i].createdatetime+"</td>"+
				                                  "<td class=\"td-status\">";
				                                  if(user.data[i].state===1){
				                                  	str += "<span class=\"layui-btn layui-btn-normal layui-btn-mini\">已启用</span></td>";
				                                  }else
				                                  {
				                                  	str += "<span class=\"layui-btn layui-btn-normal layui-btn-mini layui-btn-disabled\">已停用</span>";
				                                  }
				                                    
				                                 str += "<td class=\"td-manage\">"+
				                                    "<a onclick=\"member_stop(this,"+user.data[i].account+")\" href=\"javascript:;\"  title=\"启用\">"+
				                                     " <i class=\"layui-icon\">&#xe601;</i>"+
				                                   " </a>"+
				                                    "<a title=\"编辑\"  onclick=\"adminedit("+user.data[i].account+","+user.data[i].pawd+",\'"+user.data[i].role_name+"\')\" href=\"javascript:;\">"+
				                                     " <i class=\"layui-icon\">&#xe642;</i>"+
				                                    "</a>"+
				                                    "<a title=\"删除\" onclick=\"member_del(this,"+user.data[i].account+")\" href=\"javascript:;\">"+
				                                      "<i class=\"layui-icon\">&#xe640;</i></a></td></tr>";
				                                     
												}
													
											}
											
											$("#glytj").html(str);
											paging (max)
											
									  	 }
										 
										})	
										 
                                	}
                                	
                                	function adminedit (account,pawd,role) 
                                	{ 
                                		
                                	
                         
                                		xadmin.open('编辑','./admin-edit.html',600,400)                   
                                		localStorage.setItem("account",account);
                                	    localStorage.setItem("pawd",pawd);
                                	    localStorage.setItem("role",role);
                                		
                                	}
                                	
                                	//分页样式
                                	function paging (max) 
                                	{
                                		
								       
                                		var list =  "<div class=\"page\" ><div>"+
	                                 	"<a class=\"prev\" onclick=\"prev ()\">&lt;&lt;</a>";
                                		if(max%9==0)
                                		{
                                			 yeshu = parseInt(max/numberfenye);
                                		}else
                                		{
                                			yeshu =parseInt(max/numberfenye);
                                			yeshu +=1;
                                			
                                		}
                                		/*<span class="current">1</span>*/
                                		for (var i = 1; i <= yeshu; i++) {
                                			if(i==1)
                                			{
                                				list +=	"<a class=\"num\" style=\"background-color:#16535d\">"+i+"</a>";
                                			}else
                                			{
                                				list +=	"<a class=\"num\" >"+i+"</a>";
                                			}
                                			
                                		}
                                		list +=  "<a class=\"next\" onclick=\"next ()\">&gt;&gt;</a></div></div>";
                                		$("#fenye").html(list)
                                	}
                                	
                                		//更换标签
                                		$(document).on("click",".num",function()
                                		{
                                			 var i = $(this).text();
                                			 $(".num").not(this).css({"background-color":"white"});
											 $(this).css("background-color", "#16535d");
											 
											 //实现分页
                                			fenye (i)
                                		})
                                			
                                			 function fenye (idx)
                                			{
                                				/*alert(idx)
                                				alert(max)
                                				alert(numberfenye)
                                				alert(yeshu)*/
                                				var start = 0;
                                				var end = 0;
                                				var str = "";
                                				//当前页面应该展示哪些数据 
                                				
                                				if(idx==1)
                                				{
                                					start = 0;
                                				}else
                                				{
                                					
                                					start = (idx-1)*numberfenye;
                                				}
                                				
                                				
                                				end = idx * numberfenye-1;
                                				
                                				var aaa = eval('(' + JSON.stringify(shuju) + ')');
                                					for (var i = 0; i <max; i++) 
                                					{
													
													
													  if(start<=i && end >= i)
													  {
													  	
													  	str += "<tr>"+
				                                
				                                
				                                  "<td>"+aaa.data[i].account+"</td>"+
				                                 " <td>"+aaa.data[i].role_name+"</td>"+
				                                  "<td>"+aaa.data[i].createdatetime+"</td>"+
				                                  "<td class=\"td-status\">";
				                                  if(aaa.data[i].state===1){
				                                  	str += "<span class=\"layui-btn layui-btn-normal layui-btn-mini\">已启用</span></td>";
				                                  }else
				                                  {
				                                  	str += "<span class=\"layui-btn layui-btn-normal layui-btn-mini layui-btn-disabled\">已停用</span>";
				                                  }
				                                    
				                                 str += "<td class=\"td-manage\">"+
				                                    "<a onclick=\"member_stop(this,"+aaa.data[i].id+")\" href=\"javascript:;\"  title=\"启用\">"+
				                                     " <i class=\"layui-icon\">&#xe601;</i>"+
				                                   " </a>"+
				                                    "<a title=\"编辑\"  onclick=\"adminedit("+aaa.data[i].id+","+aaa.data[i].account+")\" href=\"javascript:;\">"+
				                                     " <i class=\"layui-icon\">&#xe642;</i>"+
				                                    "</a>"+
				                                    "<a title=\"删除\" onclick=\"member_del(this,"+aaa.data[i].id+")\" href=\"javascript:;\">"+
				                                      "<i class=\"layui-icon\">&#xe640;</i></a></td></tr>";
													  }
													  $("#glytj").html(str);
													  
													}
											  
                                				
                                			}
                                			
                                			function prev ()
                                			{
                                				fenye(1);
                             					$(".num").each(function(){
												// let this = $(this);
												if($(this).text()=="1")
												{
													$(this).css("background-color", "#16535d");
													$(".num").not(this).css({"background-color":"white"});
												}
												
											});
                                			}
                                			
                               
                                			
                                			
                                			
                                			function next ()
                                			{
                                				fenye(yeshu);
                             					$(".num").each(function(){
												// let this = $(this);
												if($(this).text()==yeshu)
												{
													$(this).css("background-color", "#16535d");
													$(".num").not(this).css({"background-color":"white"});
												}
												
											});
                                			}
                                			
                                </script>
</html>